package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.Layout
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.CateGoods
import client.comm.baoding.api.bean.Zcjl
import client.comm.baoding.databinding.LayoutCateitemNewBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.base.PagingViewHoder
import com.bumptech.glide.RequestManager
import com.google.gson.Gson

class CateNewAdapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<CateGoods.Goods>(object : DiffUtil.ItemCallback<CateGoods.Goods>() {
        val g = Gson()

        override fun areItemsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return true
        }

        override fun areContentsTheSame(
            oldItem: CateGoods.Goods,
            newItem: CateGoods.Goods
        ): Boolean {
            return g.toJson(oldItem).equals(g.toJson(newItem))
        }
    }) {

    fun itemClick(bean: CateGoods.Goods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {

        val binding = holder.binding as LayoutCateitemNewBinding
        val bean = t as CateGoods.Goods
        manager.load(bean.goods_img).into(binding.img)

        if (bean.price > 0) {
            binding.tvPrice.text = "" + bean.price
            binding.priceShow = true

            if (bean.integral > 0) {
                binding.integral = "+" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }
        } /*else if (bean.lpq_price > 0) {
            TextTool.getBuilder("${bean.lpq_price}")
                .setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(18)
                .append("体验券").setForegroundColor(Color.parseColor("#E81837"))
                .setTextSize(12)
                .into(binding.tvPrice)
            binding.priceShow = true

            if (bean.integral > 0) {
                binding.integral = "+" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }

        }*/ else {
            binding.tvPrice.text = ""
            binding.priceShow = false

            if (bean.integral > 0) {
                binding.integral = "" + bean.integral //+ "积分"
                binding.integralShow = true
            } else {
                binding.integral = ""
                binding.integralShow = false
            }
        }


        /*manager.load(bean.goods_img).diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .skipMemoryCache(true).into(binding.img)*/
        binding.eventn = this
        binding.bean = bean
    }

    override fun getItemLayout(): Int {
        return R.layout.layout_cateitem_new
    }


}